<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 15:46
 */

/**
 * 获取某节点下的栏目数组
 * @param int $parent 节点
 * @return mixed
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_nav_array($parent = 0)
{
    $nav = \think\Db::name('category')
        ->field('id, parentid, title, other_name, description, thumb, url, type, seo_title, seo_keywords, seo_desc, list_tpl, article_tpl, sort, attr, create_time, update_time')
        ->where('is_show', 1)
        ->order('sort asc, create_time desc')
        ->select();
    foreach ($nav as $key => $value) {
        if (empty($value['url']))
        {
            if ($value['type'] == 1)
            {
                $nav[$key]['url'] = url('mobile/news/index', ['id'=>$value['id']]);
            } else {
                $nav[$key]['url'] = url('mobile/info/index', ['id'=>$value['id']]);
            }
        }
    }
    $tree = new \other\Tree($nav);
    $list = $tree->get_tree($parent);
    return $list;
}

/**
 * 获取某个分类下的文章数组
 * @param $category_id 分类id
 * @param string $page 分页数量 默认是0即不分页
 * @param string $attr 文章属性
 * @param string $order 排序
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_article_array($category_id, $page = '', $attr = '', $order = 'sort desc')
{
    $pages = '';
    $where['category_id'] = $category_id;
    $where['del_status']  = 0;
    $where['status']      = 1;
    $order_where          = 'is_top desc'.$order;
    if ($page)
    {
        if ($attr)
        {
            $list  = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('attr', 'exp', 'FIND_IN_SET('.$attr.', attr)')
                ->order($order_where)
                ->paginate($page);
            $pages = $list->render();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->paginate($page);
            $pages = $list->render();
        }
    } else {
        if ($attr)
        {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->where('attr', 'exp', 'FIND_IN_SET('.$attr.', attr)')
                ->order($order_where)
                ->select();
        } else {
            $list = \think\Db::name('article')
                ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
                ->where($where)
                ->order($order_where)
                ->select();
        }
    }
    return ['list'=>$list, 'pages'=>$pages];
}

/**
 * 获取上一篇文章
 * @param $article_id 当前文章id
 * @param string $attr 属性
 * @param string $order 排序方式
 * @return array|false|PDOStatement|string|\think\Model
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_prev_article($article_id, $attr='', $order='sort desc')
{
    $article = \think\Db::name('article')
        ->field('id, category_id')
        ->where('id', $article_id)
        ->find();
    $where['category_id'] = $article['category_id'];
    $where['del_status']  = 0;
    $where['status']      = 1;
    $where['id']          = ['<', $article_id];
    if ($attr)
    {
        $where['attr']    = ['exp', 'FIND_IN_SET('.$attr.', attr)'];
    }
    $prev = \think\Db::name('article')
        ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
        ->where($where)
        ->order('is_top desc, '.$order)
        ->find();
    return !empty($prev) ? $prev : [];
}

/**
 * 获取下一篇文章
 * @param $article_id 当前文章id
 * @param string $attr 属性
 * @param string $order 排序方式
 * @return array|false|PDOStatement|string|\think\Model
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function get_next_article($article_id, $attr='', $order='sort desc')
{
    $article = \think\Db::name('article')
        ->field('id, category_id')
        ->where('id', $article_id)
        ->find();
    $where['category_id'] = $article['category_id'];
    $where['del_status']  = 0;
    $where['status']      = 1;
    $where['id']          = ['>', $article_id];
    if ($attr)
    {
        $where['attr']    = ['exp', 'FIND_IN_SET('.$attr.', attr)'];
    }
    $next = \think\Db::name('article')
        ->field('id, category_id, title, thumb, other_title, keywords, description, author, source, hits, content, sort, photos, enclosure, is_top, tpl, create_time, update_time')
        ->where($where)
        ->order('is_top desc, '.$order)
        ->find();
    return !empty($next) ? $next : [];
}